home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kgdb-4.5 / ds3100.md / gdb / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-10  |  58.7 KB  |  2,327 lines

  1. /* Top level `main' program for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "gdbcmd.h"
  23. #include "call-cmds.h"
  24. #include "symtab.h"
  25. #include "inferior.h"
  26. #include "signals.h"
  27. #include "target.h"
  28. #include "breakpoint.h"
  29. #include "gdbtypes.h"
  30. #include "expression.h"
  31. #include "language.h"
  32.  
  33. #include "getopt.h"
  34.  
  35. /* readline include files */
  36. #include "readline.h"
  37. #include "history.h"
  38.  
  39. /* readline defines this.  */
  40. #undef savestring
  41.  
  42. #ifdef USG
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. #endif
  46.  
  47. #include <string.h>
  48. #ifndef    NO_SYS_FILE
  49. #include <sys/file.h>
  50. #endif
  51. #include <setjmp.h>
  52. #include <sys/param.h>
  53. #include <sys/stat.h>
  54. #include <ctype.h>
  55.  
  56. #ifdef SET_STACK_LIMIT_HUGE
  57. #include <sys/time.h>
  58. #include <sys/resource.h>
  59.  
  60. int original_stack_limit;
  61. #endif
  62.  
  63. /* Prototypes for local functions */
  64.  
  65. static char *
  66. symbol_completion_function PARAMS ((char *, int));
  67.  
  68. static void
  69. command_loop PARAMS ((void));
  70.  
  71. static void
  72. command_loop_marker PARAMS ((int));
  73.  
  74. static void
  75. print_gdb_version PARAMS ((void));
  76.  
  77. static void
  78. quit_command PARAMS ((char *, int));
  79.  
  80. static void
  81. initialize_main PARAMS ((void));
  82.  
  83. static void
  84. initialize_history PARAMS ((void));
  85.  
  86. static void
  87. initialize_cmd_lists PARAMS ((void));
  88.  
  89. static void
  90. float_handler PARAMS ((int));
  91.  
  92. static void
  93. source_command PARAMS ((char *, int));
  94.  
  95. static void
  96. cd_command PARAMS ((char *, int));
  97.  
  98. static void
  99. print_gnu_advertisement PARAMS ((void));
  100.  
  101. static void
  102. init_signals PARAMS ((void));
  103.  
  104. static void
  105. read_command_file PARAMS ((FILE *));
  106.  
  107. static void 
  108. set_verbose PARAMS ((char *, int, struct cmd_list_element *));
  109.  
  110. static void
  111. show_history PARAMS ((char *, int));
  112.  
  113. static void
  114. set_history PARAMS ((char *, int));
  115.  
  116. static void
  117. set_history_size_command PARAMS ((char *, int, struct cmd_list_element *));
  118.  
  119. static void
  120. show_commands PARAMS ((char *, int));
  121.  
  122. static void
  123. dump_me_command PARAMS ((char *, int));
  124.  
  125. static void
  126. echo_command PARAMS ((char *, int));
  127.  
  128. static void
  129. pwd_command PARAMS ((char *, int));
  130.  
  131. static void
  132. show_version PARAMS ((char *, int));
  133.  
  134. static void
  135. document_command PARAMS ((char *, int));
  136.  
  137. static void
  138. define_command PARAMS ((char *, int));
  139.  
  140. static void
  141. validate_comname PARAMS ((char *));
  142.  
  143. static void
  144. help_command PARAMS ((char *, int));
  145.  
  146. static void
  147. show_command PARAMS ((char *, int));
  148.  
  149. static void
  150. info_command PARAMS ((char *, int));
  151.  
  152. static void
  153. do_nothing PARAMS ((int));
  154.  
  155. static void
  156. disconnect PARAMS ((int));
  157.  
  158. static void
  159. source_cleanup PARAMS ((FILE *));
  160.  
  161. /* If this definition isn't overridden by the header files, assume
  162.    that isatty and fileno exist on this system.  */
  163. #ifndef ISATTY
  164. #define ISATTY(FP)    (isatty (fileno (FP)))
  165. #endif
  166.  
  167. /* Initialization file name for gdb.  This is overridden in some configs.  */
  168.  
  169. #ifndef    GDBINIT_FILENAME
  170. #define    GDBINIT_FILENAME    ".gdbinit"
  171. #endif
  172. char gdbinit[] = GDBINIT_FILENAME;
  173.  
  174. #define    ALL_CLEANUPS    ((struct cleanup *)0)
  175.  
  176. /* Version number of GDB, as a string.  */
  177.  
  178. extern char *version;
  179.  
  180. /* Message to be printed before the error message, when an error occurs.  */
  181.  
  182. extern char *error_pre_print;
  183.  
  184. /* Message to be printed before the warning message, when a warning occurs.  */
  185.  
  186. extern char *warning_pre_print;
  187.  
  188. extern char lang_frame_mismatch_warn[];        /* language.c */
  189.  
  190. /* Whether GDB's stdin is on a terminal.  */
  191.  
  192. extern int gdb_has_a_terminal;            /* inflow.c */
  193.  
  194. /* Flag for whether we want all the "from_tty" gubbish printed.  */
  195.  
  196. int caution = 1;            /* Default is yes, sigh. */
  197.  
  198. /*
  199.  * Define all cmd_list_element's
  200.  */
  201.  
  202. /* Chain containing all defined commands.  */
  203.  
  204. struct cmd_list_element *cmdlist;
  205.  
  206. /* Chain containing all defined info subcommands.  */
  207.  
  208. struct cmd_list_element *infolist;
  209.  
  210. /* Chain containing all defined enable subcommands. */
  211.  
  212. struct cmd_list_element *enablelist;
  213.  
  214. /* Chain containing all defined disable subcommands. */
  215.  
  216. struct cmd_list_element *disablelist;
  217.  
  218. /* Chain containing all defined delete subcommands. */
  219.  
  220. struct cmd_list_element *deletelist;
  221.  
  222. /* Chain containing all defined "enable breakpoint" subcommands. */
  223.  
  224. struct cmd_list_element *enablebreaklist;
  225.  
  226. /* Chain containing all defined set subcommands */
  227.  
  228. struct cmd_list_element *setlist;
  229.  
  230. /* Chain containing all defined show subcommands.  */
  231. struct cmd_list_element *showlist;
  232.  
  233. /* Chain containing all defined \"set history\".  */
  234.  
  235. struct cmd_list_element *sethistlist;
  236.  
  237. /* Chain containing all defined \"show history\".  */
  238. struct cmd_list_element *showhistlist;
  239.  
  240. /* Chain containing all defined \"unset history\".  */
  241.  
  242. struct cmd_list_element *unsethistlist;
  243.  
  244. /* stdio stream that command input is being read from.  */
  245.  
  246. FILE *instream;
  247.  
  248. /* Current working directory.  */
  249.  
  250. char *current_directory;
  251.  
  252. /* The directory name is actually stored here (usually).  */
  253. static char dirbuf[1024];
  254.  
  255. /* Function to call before reading a command, if nonzero.
  256.    The function receives two args: an input stream,
  257.    and a prompt string.  */
  258.  
  259. void (*window_hook) PARAMS ((FILE *, char *));
  260.  
  261. extern int frame_file_full_name;
  262. extern int mapped_symbol_files;
  263. extern int readnow_symbol_files;
  264.  
  265. extern unsigned int instCount;
  266.  
  267. int epoch_interface;
  268. int xgdb_verbose;
  269.  
  270. /* gdb prints this when reading a command interactively */
  271. static char *prompt;
  272.  
  273. /* Buffer used for reading command lines, and the size
  274.    allocated for it so far.  */
  275.  
  276. char *line;
  277. int linesize = 100;
  278.  
  279. /* Baud rate specified for talking to serial target systems.  Default
  280.    is left as a zero pointer, so targets can choose their own defaults.  */
  281.  
  282. char *baud_rate;
  283.  
  284. /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
  285.  
  286. #ifndef STOP_SIGNAL
  287. #ifdef SIGTSTP
  288. #define STOP_SIGNAL SIGTSTP
  289. static void stop_sig PARAMS ((int));
  290. #endif
  291. #endif
  292.  
  293. /* Some System V have job control but not sigsetmask(). */
  294. #if !defined (HAVE_SIGSETMASK)
  295. #define HAVE_SIGSETMASK !defined (USG)
  296. #endif
  297.  
  298. #if 0 == (HAVE_SIGSETMASK)
  299. #define sigsetmask(n)
  300. #endif
  301.  
  302. /* This is how `error' returns to command level.  */
  303.  
  304. jmp_buf to_top_level;
  305.  
  306. NORETURN void
  307. return_to_top_level ()
  308. {
  309.   quit_flag = 0;
  310.   immediate_quit = 0;
  311.   bpstat_clear_actions(stop_bpstat);    /* Clear queued breakpoint commands */
  312.   disable_current_display ();
  313.   do_cleanups (ALL_CLEANUPS);
  314.   (NORETURN void) longjmp (to_top_level, 1);
  315. }
  316.  
  317. /* Call FUNC with arg ARGS, catching any errors.
  318.    If there is no error, return the value returned by FUNC.
  319.    If there is an error, print ERRSTRING, print the specific error message,
  320.                  then return zero.  */
  321.  
  322. int
  323. catch_errors (func, args, errstring)
  324.      int (*func) PARAMS ((char *));
  325.      char *args;
  326.      char *errstring;
  327. {
  328.   jmp_buf saved;
  329.   int val;
  330.   struct cleanup *saved_cleanup_chain;
  331.   char *saved_error_pre_print;
  332.  
  333.   saved_cleanup_chain = save_cleanups ();
  334.   saved_error_pre_print = error_pre_print;
  335.  
  336.   memcpy ((char *)saved, (char *)to_top_level, sizeof (jmp_buf));
  337.   error_pre_print = errstring;
  338.  
  339.   if (setjmp (to_top_level) == 0)
  340.     val = (*func) (args);
  341.   else
  342.     val = 0;
  343.  
  344.   restore_cleanups (saved_cleanup_chain);
  345.  
  346.   error_pre_print = saved_error_pre_print;
  347.   memcpy ((char *)to_top_level, (char *)saved, sizeof (jmp_buf));
  348.   return val;
  349. }
  350.  
  351. /* Handler for SIGHUP.  */
  352.  
  353. static void
  354. disconnect (signo)
  355. int signo;
  356. {
  357.   kill_inferior_fast ();
  358.   signal (signo, SIG_DFL);
  359.   kill (getpid (), SIGHUP);
  360. }
  361.  
  362. /* Clean up on error during a "source" command (or execution of a
  363.    user-defined command).  */
  364.  
  365. static void
  366. source_cleanup (stream)
  367.      FILE *stream;
  368. {
  369.   /* Restore the previous input stream.  */
  370.   instream = stream;
  371. }
  372.  
  373. /* Read commands from STREAM.  */
  374. static void
  375. read_command_file (stream)
  376.      FILE *stream;
  377. {
  378.   struct cleanup *cleanups;
  379.  
  380.   cleanups = make_cleanup (source_cleanup, instream);
  381.   instream = stream;
  382.   command_loop ();
  383.   do_cleanups (cleanups);
  384. }
  385.  
  386.  
  387. int
  388. main (argc, argv)
  389.      int argc;
  390.      char **argv;
  391. {
  392.   int count;
  393.   static int inhibit_gdbinit = 0;
  394.   static int quiet = 0;
  395.   static int batch = 0;
  396.  
  397.   /* Pointers to various arguments from command line.  */
  398.   char *symarg = NULL;
  399.   char *execarg = NULL;
  400.   char *corearg = NULL;
  401.   char *cdarg = NULL;
  402.   char *ttyarg = NULL;
  403.  
  404.   /* Pointers to all arguments of +command option.  */
  405.   char **cmdarg;
  406.   /* Allocated size of cmdarg.  */
  407.   int cmdsize;
  408.   /* Number of elements of cmdarg used.  */
  409.   int ncmd;
  410.  
  411.   /* Indices of all arguments of +directory option.  */
  412.   char **dirarg;
  413.   /* Allocated size.  */
  414.   int dirsize;
  415.   /* Number of elements used.  */
  416.   int ndir;
  417.   
  418.   register int i;
  419.  
  420.   /* This needs to happen before the first use of malloc.  */
  421.   init_malloc ((PTR) NULL);
  422.  
  423. #if defined (ALIGN_STACK_ON_STARTUP)
  424.   i = (int) &count & 0x3;
  425.   if (i != 0)
  426.     alloca (4 - i);
  427. #endif
  428.  
  429.   /* If error() is called from initialization code, just exit */
  430.   if (setjmp (to_top_level)) {
  431.     exit(1);
  432.   }
  433.  
  434.   cmdsize = 1;
  435.   cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
  436.   ncmd = 0;
  437.   dirsize = 1;
  438.   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
  439.   ndir = 0;
  440.  
  441.   quit_flag = 0;
  442.   line = (char *) xmalloc (linesize);
  443.   line[0] = '\0';        /* Terminate saved (now empty) cmd line */
  444.   instream = stdin;
  445.  
  446.   getcwd (dirbuf, sizeof (dirbuf));
  447.   current_directory = dirbuf;
  448.  
  449. #ifdef SET_STACK_LIMIT_HUGE
  450.   {
  451.     struct rlimit rlim;
  452.  
  453.     /* Set the stack limit huge so that alloca (particularly stringtab
  454.      * in dbxread.c) does not fail. */
  455.     getrlimit (RLIMIT_STACK, &rlim);
  456.     original_stack_limit = rlim.rlim_cur;
  457.     rlim.rlim_cur = rlim.rlim_max;
  458.     setrlimit (RLIMIT_STACK, &rlim);
  459.   }
  460. #endif /* SET_STACK_LIMIT_HUGE */
  461.  
  462.   /* Parse arguments and options.  */
  463.   {
  464.     int c;
  465.     static int print_help;
  466.     /* When var field is 0, use flag field to record the equivalent
  467.        short option (or arbitrary numbers starting at 10 for those
  468.        with no equivalent).  */
  469.     static struct option long_options[] =
  470.       {
  471.     {"readnow", no_argument, &readnow_symbol_files, 1},
  472.     {"r", no_argument, &readnow_symbol_files, 1},
  473.     {"mapped", no_argument, &mapped_symbol_files, 1},
  474.     {"m", no_argument, &mapped_symbol_files, 1},
  475.     {"quiet", no_argument, &quiet, 1},
  476.     {"q", no_argument, &quiet, 1},
  477.     {"nx", no_argument, &inhibit_gdbinit, 1},
  478.     {"n", no_argument, &inhibit_gdbinit, 1},
  479.     {"batch", no_argument, &batch, 1},
  480.     {"epoch", no_argument, &epoch_interface, 1},
  481.     {"fullname", no_argument, &frame_file_full_name, 1},
  482.     {"f", no_argument, &frame_file_full_name, 1},
  483.     {"help", no_argument, &print_help, 1},
  484.     {"se", required_argument, 0, 10},
  485.     {"symbols", required_argument, 0, 's'},
  486.     {"s", required_argument, 0, 's'},
  487.     {"exec", required_argument, 0, 'e'},
  488.     {"e", required_argument, 0, 'e'},
  489.     {"core", required_argument, 0, 'c'},
  490.     {"c", required_argument, 0, 'c'},
  491.     {"command", required_argument, 0, 'x'},
  492.     {"x", required_argument, 0, 'x'},
  493.     {"directory", required_argument, 0, 'd'},
  494.     {"cd", required_argument, 0, 11},
  495.     {"tty", required_argument, 0, 't'},
  496.     {"baud", required_argument, 0, 'b'},
  497.     {"b", required_argument, 0, 'b'},
  498. /* Allow machine descriptions to add more options... */
  499. #ifdef ADDITIONAL_OPTIONS
  500.     ADDITIONAL_OPTIONS
  501. #endif
  502.     {0, no_argument, 0, 0},
  503.       };
  504.  
  505.     while (1)
  506.       {
  507.     int option_index;
  508.  
  509.     c = getopt_long_only (argc, argv, "",
  510.                   long_options, &option_index);
  511.     if (c == EOF)
  512.       break;
  513.  
  514.     /* Long option that takes an argument.  */
  515.     if (c == 0 && long_options[option_index].flag == 0)
  516.       c = long_options[option_index].val;
  517.  
  518.     switch (c)
  519.       {
  520.       case 0:
  521.         /* Long option that just sets a flag.  */
  522.         break;
  523.       case 10:
  524.         symarg = optarg;
  525.         execarg = optarg;
  526.         break;
  527.       case 11:
  528.         cdarg = optarg;
  529.         break;
  530.       case 's':
  531.         symarg = optarg;
  532.         break;
  533.       case 'e':
  534.         execarg = optarg;
  535.         break;
  536.       case 'c':
  537.         corearg = optarg;
  538.         break;
  539.       case 'x':
  540.         cmdarg[ncmd++] = optarg;
  541.         if (ncmd >= cmdsize)
  542.           {
  543.         cmdsize *= 2;
  544.         cmdarg = (char **) xrealloc ((char *)cmdarg,
  545.                          cmdsize * sizeof (*cmdarg));
  546.           }
  547.         break;
  548.       case 'd':
  549.         dirarg[ndir++] = optarg;
  550.         if (ndir >= dirsize)
  551.           {
  552.         dirsize *= 2;
  553.         dirarg = (char **) xrealloc ((char *)dirarg,
  554.                          dirsize * sizeof (*dirarg));
  555.           }
  556.         break;
  557.       case 't':
  558.         ttyarg = optarg;
  559.         break;
  560.       case 'q':
  561.         quiet = 1;
  562.         break;
  563.       case 'b':
  564.         baud_rate = optarg;
  565.         break;
  566. #ifdef ADDITIONAL_OPTION_CASES
  567.       ADDITIONAL_OPTION_CASES
  568. #endif
  569.       case '?':
  570.         fprintf (stderr,
  571.              "Use `%s +help' for a complete list of options.\n",
  572.              argv[0]);
  573.         exit (1);
  574.       }
  575.  
  576.       }
  577.     if (print_help)
  578.       {
  579.     fputs ("\
  580. This is GDB, the GNU debugger.  Use the command\n\
  581.     gdb [options] [executable [core-file]]\n\
  582. to enter the debugger.\n\
  583. \n\
  584. Options available are:\n\
  585.   -help             Print this message.\n\
  586.   -quiet            Do not print version number on startup.\n\
  587.   -fullname         Output information used by emacs-GDB interface.\n\
  588.   -epoch            Output information used by epoch emacs-GDB interface.\n\
  589.   -batch            Exit after processing options.\n\
  590.   -nx               Do not read .gdbinit file.\n\
  591.   -tty=TTY          Use TTY for input/output by the program being debugged.\n\
  592.   -cd=DIR           Change current directory to DIR.\n\
  593.   -directory=DIR    Search for source files in DIR.\n\
  594.   -command=FILE     Execute GDB commands from FILE.\n\
  595.   -symbols=SYMFILE  Read symbols from SYMFILE.\n\
  596.   -exec=EXECFILE    Use EXECFILE as the executable.\n\
  597.   -se=FILE          Use FILE as symbol file and executable file.\n\
  598.   -core=COREFILE    Analyze the core dump COREFILE.\n\
  599.   -b BAUDRATE       Set serial port baud rate used for remote debugging.\n\
  600.   -mapped           Use mapped symbol files if supported on this system.\n\
  601.   -readnow          Fully read symbol files on first access.\n\
  602. ", stderr);
  603. #ifdef ADDITIONAL_OPTION_HELP
  604.     fputs (ADDITIONAL_OPTION_HELP, stderr);
  605. #endif
  606.     fputs ("\n\
  607. For more information, type \"help\" from within GDB, or consult the\n\
  608. GDB manual (available as on-line info or a printed manual).\n", stderr);
  609.     /* Exiting after printing this message seems like
  610.        the most useful thing to do.  */
  611.     exit (0);
  612.       }
  613.     
  614.     /* OK, that's all the options.  The other arguments are filenames.  */
  615.     count = 0;
  616.     for (; optind < argc; optind++)
  617.       switch (++count)
  618.     {
  619.     case 1:
  620.       symarg = argv[optind];
  621.       execarg = argv[optind];
  622.       break;
  623.     case 2:
  624.       corearg = argv[optind];
  625.       break;
  626.     case 3:
  627.       fprintf (stderr,
  628.            "Excess command line arguments ignored. (%s%s)\n",
  629.            argv[optind], (optind == argc - 1) ? "" : " ...");
  630.       break;
  631.     }
  632.     if (batch)
  633.       quiet = 1;
  634.   }
  635.  
  636.   /* Run the init function of each source file */
  637.  
  638.   initialize_cmd_lists ();    /* This needs to be done first */
  639.   initialize_all_files ();
  640.   initialize_main ();        /* But that omits this file!  Do it now */
  641.   init_signals ();
  642.  
  643.   if (!quiet)
  644.     {
  645.       /* Print all the junk at the top, with trailing "..." if we are about
  646.      to read a symbol file (possibly slowly).  */
  647.       print_gnu_advertisement ();
  648.       print_gdb_version ();
  649.       if (symarg)
  650.     printf_filtered ("..");
  651.       wrap_here("");
  652.       fflush (stdout);        /* Force to screen during slow operations */
  653.     }
  654.  
  655.   error_pre_print = "\n\n";
  656.   warning_pre_print = "\n\nwarning: ";
  657.  
  658.   /* Now perform all the actions indicated by the arguments.  */
  659.   if (cdarg != NULL)
  660.     {
  661.       if (!setjmp (to_top_level))
  662.     {
  663.       cd_command (cdarg, 0);
  664.       init_source_path ();
  665.     }
  666.     }
  667.   do_cleanups (ALL_CLEANUPS);
  668.  
  669.   for (i = 0; i < ndir; i++)
  670.     if (!setjmp (to_top_level))
  671.       directory_command (dirarg[i], 0);
  672.   free ((PTR)dirarg);
  673.   do_cleanups (ALL_CLEANUPS);
  674.  
  675.   if (execarg != NULL
  676.       && symarg != NULL
  677.       && strcmp (execarg, symarg) == 0)
  678.     {
  679.       /* The exec file and the symbol-file are the same.  If we can't open
  680.      it, better only print one error message.  */
  681.       if (!setjmp (to_top_level))
  682.     {
  683.       exec_file_command (execarg, !batch);
  684.       symbol_file_command (symarg, 0);
  685.     }
  686.     }
  687.   else
  688.     {
  689.       if (execarg != NULL)
  690.     if (!setjmp (to_top_level))
  691.       exec_file_command (execarg, !batch);
  692.       if (symarg != NULL)
  693.     if (!setjmp (to_top_level))
  694.       symbol_file_command (symarg, 0);
  695.     }
  696.   do_cleanups (ALL_CLEANUPS);
  697.  
  698.   /* After the symbol file has been read, print a newline to get us
  699.      beyond the copyright line...  But errors should still set off
  700.      the error message with a (single) blank line.  */
  701.   if (!quiet)
  702.     printf_filtered ("\n");
  703.   error_pre_print = "\n";
  704.   warning_pre_print = "\nwarning: ";
  705.  
  706.   /* Set the initial language. */
  707.   {
  708.     struct partial_symtab *pst = find_main_psymtab ();
  709.     enum language lang = language_unknown;      
  710.     if (pst == NULL) ;
  711. #if 0
  712.     /* A better solution would set the language when reading the psymtab.
  713.        This would win for symbol file formats that encode the langauge,
  714.        such as DWARF.  But, we don't do that yet. FIXME */
  715.     else if (pst->language != language_unknown)
  716.     lang = pst->language;
  717. #endif
  718.     else if (pst->filename != NULL)
  719.       lang = deduce_language_from_filename (pst->filename);
  720.     if (lang == language_unknown) /* Make C the default language */
  721.     lang = language_c;
  722.     set_language (lang);
  723.   }
  724.  
  725.   if (corearg != NULL)
  726.     if (!setjmp (to_top_level))
  727.       core_file_command (corearg, !batch);
  728.     else if (isdigit (corearg[0]) && !setjmp (to_top_level))
  729.       attach_command (corearg, !batch);
  730.   do_cleanups (ALL_CLEANUPS);
  731.  
  732.   if (ttyarg != NULL)
  733.     if (!setjmp (to_top_level))
  734.       tty_command (ttyarg, !batch);
  735.   do_cleanups (ALL_CLEANUPS);
  736.  
  737. #ifdef ADDITIONAL_OPTION_HANDLER
  738.   ADDITIONAL_OPTION_HANDLER;
  739. #endif
  740.  
  741.   /* Error messages should no longer be distinguished with extra output. */
  742.   error_pre_print = 0;
  743.   warning_pre_print = "warning: ";
  744.  
  745.   {
  746.     struct stat homebuf, cwdbuf;
  747.     char *homedir, *homeinit;
  748.  
  749.     /* Read init file, if it exists in home directory  */
  750.     homedir = getenv ("HOME");
  751.     if (homedir)
  752.       {
  753.     homeinit = (char *) alloca (strlen (getenv ("HOME")) +
  754.                     strlen (gdbinit) + 10);
  755.     strcpy (homeinit, getenv ("HOME"));
  756.     strcat (homeinit, "/");
  757.     strcat (homeinit, gdbinit);
  758.     if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
  759.       if (!setjmp (to_top_level))
  760.         source_command (homeinit, 0);
  761.     do_cleanups (ALL_CLEANUPS);
  762.  
  763.     /* Do stats; no need to do them elsewhere since we'll only
  764.        need them if homedir is set.  Make sure that they are
  765.        zero in case one of them fails (this guarantees that they
  766.        won't match if either exists).  */
  767.     
  768.     memset (&homebuf, 0, sizeof (struct stat));
  769.     memset (&cwdbuf, 0, sizeof (struct stat));
  770.     
  771.     stat (homeinit, &homebuf);
  772.     stat (gdbinit, &cwdbuf); /* We'll only need this if
  773.                      homedir was set.  */
  774.       }
  775.     
  776.     /* Read the input file in the current directory, *if* it isn't
  777.        the same file (it should exist, also).  */
  778.  
  779.     if (!homedir
  780.     || bcmp ((char *) &homebuf,
  781.          (char *) &cwdbuf,
  782.          sizeof (struct stat)))
  783.       if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
  784.     if (!setjmp (to_top_level))
  785.       source_command (gdbinit, 0);
  786.     do_cleanups (ALL_CLEANUPS);
  787.   }
  788.  
  789.   for (i = 0; i < ncmd; i++)
  790.     if (!setjmp (to_top_level))
  791.       {
  792.     if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
  793.       read_command_file (stdin);
  794.     else
  795.       source_command (cmdarg[i], !batch);
  796.     do_cleanups (ALL_CLEANUPS);
  797.       }
  798.   free ((PTR)cmdarg);
  799.  
  800.   /* Read in the old history after all the command files have been read. */
  801.   initialize_history();
  802.  
  803.   if (batch)
  804.     {
  805.       /* We have hit the end of the batch file.  */
  806.       exit (0);
  807.     }
  808.  
  809.   /* Do any host- or target-specific hacks.  This is used for i960 targets
  810.      to force the user to set a nindy target and spec its parameters.  */
  811.  
  812. #ifdef BEFORE_MAIN_LOOP_HOOK
  813.   BEFORE_MAIN_LOOP_HOOK;
  814. #endif
  815.  
  816.   /* The command loop.  */
  817.  
  818.   while (1)
  819.     {
  820.       if (!setjmp (to_top_level))
  821.     {
  822.       do_cleanups (ALL_CLEANUPS);        /* Do complete cleanup */
  823.       command_loop ();
  824.           quit_command ((char *)0, instream == stdin);
  825.     }
  826.     }
  827.   /* No exit -- exit is through quit_command.  */
  828. }
  829.  
  830. /* Execute the line P as a command.
  831.    Pass FROM_TTY as second argument to the defining function.  */
  832.  
  833. void
  834. execute_command (p, from_tty)
  835.      char *p;
  836.      int from_tty;
  837. {
  838.   register struct cmd_list_element *c;
  839.   register struct command_line *cmdlines;
  840.   register enum language flang;
  841.   static const struct language_defn *saved_language = 0;
  842.   static int warned = 0;
  843.  
  844.   free_all_values ();
  845.  
  846.   /* This can happen when command_line_input hits end of file.  */
  847.   if (p == NULL)
  848.       return;
  849.   
  850.   while (*p == ' ' || *p == '\t') p++;
  851.   if (*p)
  852.     {
  853.       char *arg;
  854.       
  855.       c = lookup_cmd (&p, cmdlist, "", 0, 1);
  856.       /* Pass null arg rather than an empty one.  */
  857.       arg = *p ? p : 0;
  858.       if (c->class == class_user)
  859.     {
  860.       struct cleanup *old_chain;
  861.       
  862.       if (*p)
  863.         error ("User-defined commands cannot take arguments.");
  864.       cmdlines = c->user_commands;
  865.       if (cmdlines == 0)
  866.         /* Null command */
  867.         return;
  868.  
  869.       /* Set the instream to 0, indicating execution of a
  870.          user-defined function.  */
  871.       old_chain =  make_cleanup (source_cleanup, instream);
  872.       instream = (FILE *) 0;
  873.       while (cmdlines)
  874.         {
  875.           execute_command (cmdlines->line, 0);
  876.           cmdlines = cmdlines->next;
  877.         }
  878.       do_cleanups (old_chain);
  879.     }
  880.       else if (c->type == set_cmd || c->type == show_cmd)
  881.     do_setshow_command (arg, from_tty & caution, c);
  882.       else if (c->function.cfunc == NO_FUNCTION)
  883.     error ("That is not a command, just a help topic.");
  884.       else
  885.     (*c->function.cfunc) (arg, from_tty & caution);
  886.    }
  887.  
  888.   /* Tell the user if the language has changed (except first time).  */
  889.   if (current_language != saved_language)
  890.   {
  891.     if (language_mode == language_mode_auto) {
  892.       if (saved_language)
  893.     language_info (1);    /* Print what changed.  */
  894.     }
  895.     saved_language = current_language;
  896.     warned = 0;
  897.   }
  898.  
  899.   /* Warn the user if the working language does not match the
  900.      language of the current frame.  Only warn the user if we are
  901.      actually running the program, i.e. there is a stack. */
  902.   /* FIXME:  This should be cacheing the frame and only running when
  903.      the frame changes.  */
  904.   if (target_has_stack)
  905.   {
  906.     flang = get_frame_language ();
  907.     if (!warned
  908.         && flang != language_unknown
  909.     && flang != current_language->la_language)
  910.     {
  911.       printf_filtered ("%s\n", lang_frame_mismatch_warn);
  912.       warned = 1;
  913.     }
  914.   }
  915. }
  916.  
  917. /* ARGSUSED */
  918. static void
  919. command_loop_marker (foo)
  920.      int foo;
  921. {
  922. }
  923.  
  924. /* Read commands from `instream' and execute them
  925.    until end of file or error reading instream.  */
  926. static void
  927. command_loop ()
  928. {
  929.   struct cleanup *old_chain;
  930.   char *command;
  931.   int stdin_is_tty = ISATTY (stdin);
  932.  
  933.   while (!feof (instream))
  934.     {
  935.       if (window_hook && instream == stdin)
  936.     (*window_hook) (instream, prompt);
  937.  
  938.       quit_flag = 0;
  939.       if (instream == stdin && stdin_is_tty)
  940.     reinitialize_more_filter ();
  941.       old_chain = make_cleanup (command_loop_marker, 0);
  942.       command = command_line_input (instream == stdin ? prompt : (char *) NULL,
  943.                       instream == stdin);
  944.       if (command == 0)
  945.     return;
  946.       execute_command (command, instream == stdin);
  947.       /* Do any commands attached to breakpoint we stopped at.  */
  948.       bpstat_do_actions (&stop_bpstat);
  949.       do_cleanups (old_chain);
  950.     }
  951. }
  952.  
  953. /* Commands call this if they do not want to be repeated by null lines.  */
  954.  
  955. void
  956. dont_repeat ()
  957. {
  958.   /* If we aren't reading from standard input, we are saving the last
  959.      thing read from stdin in line and don't want to delete it.  Null lines
  960.      won't repeat here in any case.  */
  961.   if (instream == stdin)
  962.     *line = 0;
  963. }
  964.  
  965. /* Read a line from the stream "instream" without command line editing.
  966.  
  967.    It prints PRROMPT once at the start.
  968.    Action is compatible with "readline", e.g. space for the result is 
  969.    malloc'd and should be freed by the caller.
  970.  
  971.    A NULL return means end of file.  */
  972. char *
  973. gdb_readline (prrompt)
  974.      char *prrompt;
  975. {
  976.   int c;
  977.   char *result;
  978.   int input_index = 0;
  979.   int result_size = 80;
  980.  
  981.   if (prrompt)
  982.     {
  983.       printf (prrompt);
  984.       fflush (stdout);
  985.     }
  986.   
  987.   result = (char *) xmalloc (result_size);
  988.  
  989.   while (1)
  990.     {
  991.       /* Read from stdin if we are executing a user defined command.
  992.      This is the right thing for prompt_for_continue, at least.  */
  993.       c = fgetc (instream ? instream : stdin);
  994.  
  995.       if (c == EOF)
  996.     {
  997.       free (result);
  998.       return NULL;
  999.     }
  1000.  
  1001.       if (c == '\n')
  1002.     break;
  1003.  
  1004.       result[input_index++] = c;
  1005.       while (input_index >= result_size)
  1006.     {
  1007.       result_size *= 2;
  1008.       result = (char *) xrealloc (result, result_size);
  1009.     }
  1010.     }
  1011.  
  1012.   result[input_index++] = '\0';
  1013.   return result;
  1014. }
  1015.  
  1016. /* Variables which control command line editing and history
  1017.    substitution.  These variables are given default values at the end
  1018.    of this file.  */
  1019. static int command_editing_p;
  1020. static int history_expansion_p;
  1021. static int write_history_p;
  1022. static int history_size;
  1023. static char *history_filename;
  1024.  
  1025. /* Variables which are necessary for fancy command line editing.  */
  1026. char *gdb_completer_word_break_characters =
  1027.   " \t\n!@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
  1028.  
  1029. /* Functions that are used as part of the fancy command line editing.  */
  1030.  
  1031. /* This can be used for functions which don't want to complete on symbols
  1032.    but don't want to complete on anything else either.  */
  1033. /* ARGSUSED */
  1034. char **
  1035. noop_completer (text)
  1036.      char *text;
  1037. {
  1038.   return NULL;
  1039. }
  1040.  
  1041. /* Generate symbol names one by one for the completer.  If STATE is
  1042.    zero, then we need to initialize, otherwise the initialization has
  1043.    already taken place.  TEXT is what we expect the symbol to start
  1044.    with.  RL_LINE_BUFFER is available to be looked at; it contains the
  1045.    entire text of the line.  RL_POINT is the offset in that line of
  1046.    the cursor.  You should pretend that the line ends at RL_POINT.
  1047.    The result is NULL if there are no more completions, else a char
  1048.    string which is a possible completion.  */
  1049.  
  1050. static char *
  1051. symbol_completion_function (text, state)
  1052.      char *text;
  1053.      int state;
  1054. {
  1055.   static char **list = (char **)NULL;
  1056.   static int index;
  1057.   char *output;
  1058.   extern char *rl_line_buffer;
  1059.   extern int rl_point;
  1060.   char *tmp_command, *p;
  1061.   struct cmd_list_element *c, *result_list;
  1062.  
  1063.   if (!state)
  1064.     {
  1065.       /* Free the storage used by LIST, but not by the strings inside.  This is
  1066.      because rl_complete_internal () frees the strings. */
  1067.       if (list)
  1068.     free ((PTR)list);
  1069.       list = 0;
  1070.       index = 0;
  1071.  
  1072.       /* Decide whether to complete on a list of gdb commands or on
  1073.      symbols.  */
  1074.       tmp_command = (char *) alloca (rl_point + 1);
  1075.       p = tmp_command;
  1076.       
  1077.       strncpy (tmp_command, rl_line_buffer, rl_point);
  1078.       tmp_command[rl_point] = '\0';
  1079.  
  1080.       if (rl_point == 0)
  1081.     {
  1082.       /* An empty line we want to consider ambiguous; that is,
  1083.          it could be any command.  */
  1084.       c = (struct cmd_list_element *) -1;
  1085.       result_list = 0;
  1086.     }
  1087.       else
  1088.     c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
  1089.  
  1090.       /* Move p up to the next interesting thing.  */
  1091.       while (*p == ' ' || *p == '\t')
  1092.     p++;
  1093.  
  1094.       if (!c)
  1095.     /* He's typed something unrecognizable.  Sigh.  */
  1096.     list = (char **) 0;
  1097.       else if (c == (struct cmd_list_element *) -1)
  1098.     {
  1099.       /* If we didn't recognize everything up to the thing that
  1100.          needs completing, and we don't know what command it is
  1101.          yet, we are in trouble.  Part of the trouble might be
  1102.          that the list of delimiters used by readline includes
  1103.          '-', which we use in commands.  Check for this.  */
  1104.       if (p + strlen(text) != tmp_command + rl_point) {
  1105.         if (tmp_command[rl_point - strlen(text) - 1] == '-')
  1106.           text = p;
  1107.         else {
  1108.           /* This really should not produce an error.  Better would
  1109.          be to pretend to hit RETURN here; this would produce a
  1110.          response like "Ambiguous command: foo, foobar, etc",
  1111.          and leave the line available for re-entry with ^P.  Instead,
  1112.          this error blows away the user's typed input without
  1113.          any way to get it back.  */
  1114.           error ("  Unrecognized command.");
  1115.         }
  1116.       }
  1117.       
  1118.       /* He's typed something ambiguous.  This is easier.  */
  1119.       if (result_list)
  1120.         list = complete_on_cmdlist (*result_list->prefixlist, text);
  1121.       else
  1122.         list = complete_on_cmdlist (cmdlist, text);
  1123.     }
  1124.       else
  1125.     {
  1126.       /* If we've gotten this far, gdb has recognized a full
  1127.          command.  There are several possibilities:
  1128.  
  1129.          1) We need to complete on the command.
  1130.          2) We need to complete on the possibilities coming after
  1131.          the command.
  1132.          2) We need to complete the text of what comes after the
  1133.          command.   */
  1134.  
  1135.       if (!*p && *text)
  1136.         /* Always (might be longer versions of thie command).  */
  1137.         list = complete_on_cmdlist (result_list, text);
  1138.       else if (!*p && !*text)
  1139.         {
  1140.           if (c->prefixlist)
  1141.         list = complete_on_cmdlist (*c->prefixlist, "");
  1142.           else
  1143.         list = (*c->completer) ("");
  1144.         }
  1145.       else
  1146.         {
  1147.           if (c->prefixlist && !c->allow_unknown)
  1148.         {
  1149. #if 0
  1150.           /* Something like "info adsfkdj".  But error() is not
  1151.              the proper response; just return no completions
  1152.              instead.  */
  1153.           *p = '\0';
  1154.           error ("\"%s\" command requires a subcommand.",
  1155.              tmp_command);
  1156. #else
  1157.           list = NULL;
  1158. #endif
  1159.         }
  1160.           else
  1161.         list = (*c->completer) (text);
  1162.         }
  1163.     }
  1164.     }
  1165.  
  1166.   /* If the debugged program wasn't compiled with symbols, or if we're
  1167.      clearly completing on a command and no command matches, return
  1168.      NULL.  */
  1169.   if (!list)
  1170.     return ((char *)NULL);
  1171.  
  1172.   output = list[index];
  1173.   if (output)
  1174.     index++;
  1175.  
  1176.   return (output);
  1177. }
  1178.  
  1179. #ifdef STOP_SIGNAL
  1180. static void
  1181. stop_sig (signo)
  1182. int signo;
  1183. {
  1184. #if STOP_SIGNAL == SIGTSTP
  1185.   signal (SIGTSTP, SIG_DFL);
  1186.   sigsetmask (0);
  1187.   kill (getpid (), SIGTSTP);
  1188.   signal (SIGTSTP, stop_sig);
  1189. #else
  1190.   signal (STOP_SIGNAL, stop_sig);
  1191. #endif
  1192.   printf ("%s", prompt);
  1193.   fflush (stdout);
  1194.  
  1195.   /* Forget about any previous command -- null line now will do nothing.  */
  1196.   dont_repeat ();
  1197. }
  1198. #endif /* STOP_SIGNAL */
  1199.  
  1200. /* Initialize signal handlers. */
  1201. static void
  1202. do_nothing (signo)
  1203. int signo;
  1204. {
  1205. }
  1206.  
  1207. static void
  1208. init_signals ()
  1209. {
  1210.   signal (SIGINT, request_quit);
  1211.  
  1212.   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
  1213.      passed to the inferior, which we don't want.  It would be
  1214.      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
  1215.      on BSD4.3 systems using vfork, that can affect the
  1216.      GDB process as well as the inferior (the signal handling tables
  1217.      might be in memory, shared between the two).  Since we establish
  1218.      a handler for SIGQUIT, when we call exec it will set the signal
  1219.      to SIG_DFL for us.  */
  1220.   signal (SIGQUIT, do_nothing);
  1221.   if (signal (SIGHUP, do_nothing) != SIG_IGN)
  1222.     signal (SIGHUP, disconnect);
  1223.   signal (SIGFPE, float_handler);
  1224.  
  1225. #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
  1226.   signal (SIGWINCH, SIGWINCH_HANDLER);
  1227. #endif
  1228. }
  1229.  
  1230. /* Read one line from the command input stream `instream'
  1231.    into the local static buffer `linebuffer' (whose current length
  1232.    is `linelength').
  1233.    The buffer is made bigger as necessary.
  1234.    Returns the address of the start of the line.
  1235.  
  1236.    NULL is returned for end of file.
  1237.  
  1238.    *If* the instream == stdin & stdin is a terminal, the line read
  1239.    is copied into the file line saver (global var char *line,
  1240.    length linesize) so that it can be duplicated.
  1241.  
  1242.    This routine either uses fancy command line editing or
  1243.    simple input as the user has requested.  */
  1244.  
  1245. char *
  1246. command_line_input (prrompt, repeat)
  1247.      char *prrompt;
  1248.      int repeat;
  1249. {
  1250.   static char *linebuffer = 0;
  1251.   static unsigned linelength = 0;
  1252.   register char *p;
  1253.   char *p1;
  1254.   char *rl;
  1255.   char *local_prompt = prrompt;
  1256.   register int c;
  1257.   char *nline;
  1258.   char got_eof = 0;
  1259.  
  1260.   if (linebuffer == 0)
  1261.     {
  1262.       linelength = 80;
  1263.       linebuffer = (char *) xmalloc (linelength);
  1264.     }
  1265.  
  1266.   p = linebuffer;
  1267.  
  1268.   /* Control-C quits instantly if typed while in this loop
  1269.      since it should not wait until the user types a newline.  */
  1270.   immediate_quit++;
  1271. #ifdef STOP_SIGNAL
  1272.   signal (STOP_SIGNAL, stop_sig);
  1273. #endif
  1274.  
  1275.   while (1)
  1276.     {
  1277.       /* Reports are that some Sys V's don't flush stdout/err on reads
  1278.      from stdin, when stdin/out are sockets rather than ttys.  So we
  1279.      have to do it ourselves, to make emacs-gdb and xxgdb work.
  1280.      On other machines, doing this once per input should be a cheap nop.  */
  1281.       fflush (stdout);
  1282.       fflush (stderr);
  1283.  
  1284.       /* Don't use fancy stuff if not talking to stdin.  */
  1285.       if (command_editing_p && instream == stdin
  1286.       && ISATTY (instream))
  1287.     rl = readline (local_prompt);
  1288.       else
  1289.     rl = gdb_readline (local_prompt);
  1290.  
  1291.       if (!rl || rl == (char *) EOF)
  1292.     {
  1293.       got_eof = 1;
  1294.       break;
  1295.     }
  1296.       if (strlen(rl) + 1 + (p - linebuffer) > linelength)
  1297.     {
  1298.       linelength = strlen(rl) + 1 + (p - linebuffer);
  1299.       nline = (char *) xrealloc (linebuffer, linelength);
  1300.       p += nline - linebuffer;
  1301.       linebuffer = nline;
  1302.     }
  1303.       p1 = rl;
  1304.       /* Copy line.  Don't copy null at end.  (Leaves line alone
  1305.          if this was just a newline)  */
  1306.       while (*p1)
  1307.     *p++ = *p1++;
  1308.  
  1309.       free (rl);            /* Allocated in readline.  */
  1310.  
  1311.       if (p == linebuffer || *(p - 1) != '\\')
  1312.     break;
  1313.  
  1314.       p--;            /* Put on top of '\'.  */
  1315.       local_prompt = (char *) 0;
  1316.   }
  1317.  
  1318. #ifdef STOP_SIGNAL
  1319.   signal (SIGTSTP, SIG_DFL);
  1320. #endif
  1321.   immediate_quit--;
  1322.  
  1323.   if (got_eof)
  1324.     return NULL;
  1325.  
  1326.   /* Do history expansion if that is wished.  */
  1327.   if (history_expansion_p && instream == stdin
  1328.       && ISATTY (instream))
  1329.     {
  1330.       char *history_value;
  1331.       int expanded;
  1332.  
  1333.       *p = '\0';        /* Insert null now.  */
  1334.       expanded = history_expand (linebuffer, &history_value);
  1335.       if (expanded)
  1336.     {
  1337.       /* Print the changes.  */
  1338.       printf ("%s\n", history_value);
  1339.  
  1340.       /* If there was an error, call this function again.  */
  1341.       if (expanded < 0)
  1342.         {
  1343.           free (history_value);
  1344.           return command_line_input (prrompt, repeat);
  1345.         }
  1346.       if (strlen (history_value) > linelength)
  1347.         {
  1348.           linelength = strlen (history_value) + 1;
  1349.           linebuffer = (char *) xrealloc (linebuffer, linelength);
  1350.         }
  1351.       strcpy (linebuffer, history_value);
  1352.       p = linebuffer + strlen(linebuffer);
  1353.       free (history_value);
  1354.     }
  1355.     }
  1356.  
  1357.   /* If we just got an empty line, and that is supposed
  1358.      to repeat the previous command, return the value in the
  1359.      global buffer.  */
  1360.   if (repeat)
  1361.     {
  1362.       if (p == linebuffer)
  1363.     return line;
  1364.       p1 = linebuffer;
  1365.       while (*p1 == ' ' || *p1 == '\t')
  1366.     p1++;
  1367.       if (!*p1)
  1368.     return line;
  1369.     }
  1370.  
  1371.   *p = 0;
  1372.  
  1373.   /* Add line to history if appropriate.  */
  1374.   if (instream == stdin
  1375.       && ISATTY (stdin) && *linebuffer)
  1376.     add_history (linebuffer);
  1377.  
  1378.   /* Note: lines consisting soley of comments are added to the command
  1379.      history.  This is useful when you type a command, and then
  1380.      realize you don't want to execute it quite yet.  You can comment
  1381.      out the command and then later fetch it from the value history
  1382.      and remove the '#'.  The kill ring is probably better, but some
  1383.      people are in the habit of commenting things out.  */
  1384.   p1 = linebuffer;
  1385.   while ((c = *p1++) != '\0')
  1386.     {
  1387.       if (c == '"')
  1388.     while ((c = *p1++) != '"')
  1389.       {
  1390.         /* Make sure an escaped '"' doesn't make us think the string
  1391.            is ended.  */
  1392.         if (c == '\\')
  1393.           parse_escape (&p1);
  1394.         if (c == '\0')
  1395.           break;
  1396.       }
  1397.       else if (c == '\'')
  1398.     while ((c = *p1++) != '\'')
  1399.       {
  1400.         /* Make sure an escaped '\'' doesn't make us think the string
  1401.            is ended.  */
  1402.         if (c == '\\')
  1403.           parse_escape (&p1);
  1404.         if (c == '\0')
  1405.           break;
  1406.       }
  1407.       else if (c == '#')
  1408.     {
  1409.       /* Found a comment.  */
  1410.       p1[-1] = '\0';
  1411.       break;
  1412.     }
  1413.     }
  1414.  
  1415.   /* Save into global buffer if appropriate.  */
  1416.   if (repeat)
  1417.     {
  1418.       if (linelength > linesize)
  1419.     {
  1420.       line = xrealloc (line, linelength);
  1421.       linesize = linelength;
  1422.     }
  1423.       strcpy (line, linebuffer);
  1424.       return line;
  1425.     }
  1426.  
  1427.   return linebuffer;
  1428. }
  1429.  
  1430. /* Read lines from the input stream
  1431.    and accumulate them in a chain of struct command_line's
  1432.    which is then returned.  */
  1433.  
  1434. struct command_line *
  1435. read_command_lines ()
  1436. {
  1437.   struct command_line *first = 0;
  1438.   register struct command_line *next, *tail = 0;
  1439.   register char *p, *p1;
  1440.   struct cleanup *old_chain = 0;
  1441.  
  1442.   while (1)
  1443.     {
  1444.       dont_repeat ();
  1445.       p = command_line_input ((char *) NULL, instream == stdin);
  1446.       if (p == NULL)
  1447.     /* Treat end of file like "end".  */
  1448.     break;
  1449.       
  1450.       /* Remove leading and trailing blanks.  */
  1451.       while (*p == ' ' || *p == '\t') p++;
  1452.       p1 = p + strlen (p);
  1453.       while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
  1454.  
  1455.       /* Is this "end"?  */
  1456.       if (p1 - p == 3 && !strncmp (p, "end", 3))
  1457.     break;
  1458.  
  1459.       /* No => add this line to the chain of command lines.  */
  1460.       next = (struct command_line *) xmalloc (sizeof (struct command_line));
  1461.       next->line = savestring (p, p1 - p);
  1462.       next->next = 0;
  1463.       if (tail)
  1464.     {
  1465.       tail->next = next;
  1466.     }
  1467.       else
  1468.     {
  1469.       /* We just read the first line.
  1470.          From now on, arrange to throw away the lines we have
  1471.          if we quit or get an error while inside this function.  */
  1472.       first = next;
  1473.       old_chain = make_cleanup (free_command_lines, &first);
  1474.     }
  1475.       tail = next;
  1476.     }
  1477.  
  1478.   dont_repeat ();
  1479.  
  1480.   /* Now we are about to return the chain to our caller,
  1481.      so freeing it becomes his responsibility.  */
  1482.   if (first)
  1483.     discard_cleanups (old_chain);
  1484.   return first;
  1485. }
  1486.  
  1487. /* Free a chain of struct command_line's.  */
  1488.  
  1489. void
  1490. free_command_lines (lptr)
  1491.       struct command_line **lptr;
  1492. {
  1493.   register struct command_line *l = *lptr;
  1494.   register struct command_line *next;
  1495.  
  1496.   while (l)
  1497.     {
  1498.       next = l->next;
  1499.       free (l->line);
  1500.       free ((PTR)l);
  1501.       l = next;
  1502.     }
  1503. }
  1504.  
  1505. /* Add an element to the list of info subcommands.  */
  1506.  
  1507. void
  1508. add_info (name, fun, doc)
  1509.      char *name;
  1510.      void (*fun) PARAMS ((char *, int));
  1511.      char *doc;
  1512. {
  1513.   add_cmd (name, no_class, fun, doc, &infolist);
  1514. }
  1515.  
  1516. /* Add an alias to the list of info subcommands.  */
  1517.  
  1518. void
  1519. add_info_alias (name, oldname, abbrev_flag)
  1520.      char *name;
  1521.      char *oldname;
  1522.      int abbrev_flag;
  1523. {
  1524.   add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  1525. }
  1526.  
  1527. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  1528.    Therefore, its own definition is called only for "info" with no args.  */
  1529.  
  1530. /* ARGSUSED */
  1531. static void
  1532. info_command (arg, from_tty)
  1533.      char *arg;
  1534.      int from_tty;
  1535. {
  1536.   printf ("\"info\" must be followed by the name of an info command.\n");
  1537.   help_list (infolist, "info ", -1, stdout);
  1538. }
  1539.  
  1540. /* The "show" command with no arguments shows all the settings.  */
  1541.  
  1542. /* ARGSUSED */
  1543. static void
  1544. show_command (arg, from_tty)
  1545.      char *arg;
  1546.      int from_tty;
  1547. {
  1548.   cmd_show_list (showlist, from_tty, "");
  1549. }
  1550.  
  1551. /* Add an element to the list of commands.  */
  1552.  
  1553. void
  1554. add_com (name, class, fun, doc)
  1555.      char *name;
  1556.      enum command_class class;
  1557.      void (*fun) PARAMS ((char *, int));
  1558.      char *doc;
  1559. {
  1560.   add_cmd (name, class, fun, doc, &cmdlist);
  1561. }
  1562.  
  1563. /* Add an alias or abbreviation command to the list of commands.  */
  1564.  
  1565. void
  1566. add_com_alias (name, oldname, class, abbrev_flag)
  1567.      char *name;
  1568.      char *oldname;
  1569.      enum command_class class;
  1570.      int abbrev_flag;
  1571. {
  1572.   add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  1573. }
  1574.  
  1575. void
  1576. error_no_arg (why)
  1577.      char *why;
  1578. {
  1579.   error ("Argument required (%s).", why);
  1580. }
  1581.  
  1582. /* ARGSUSED */
  1583. static void
  1584. help_command (command, from_tty)
  1585.      char *command;
  1586.      int from_tty; /* Ignored */
  1587. {
  1588.   help_cmd (command, stdout);
  1589. }
  1590.  
  1591. static void
  1592. validate_comname (comname)
  1593.      char *comname;
  1594. {
  1595.   register char *p;
  1596.  
  1597.   if (comname == 0)
  1598.     error_no_arg ("name of command to define");
  1599.  
  1600.   p = comname;
  1601.   while (*p)
  1602.     {
  1603.       if (!isalnum(*p) && *p != '-')
  1604.     error ("Junk in argument list: \"%s\"", p);
  1605.       p++;
  1606.     }
  1607. }
  1608.  
  1609. /* This is just a placeholder in the command data structures.  */
  1610. static void
  1611. user_defined_command (ignore, from_tty)
  1612.      char *ignore;
  1613.      int from_tty;
  1614. {
  1615. }
  1616.  
  1617. static void
  1618. define_command (comname, from_tty)
  1619.      char *comname;
  1620.      int from_tty;
  1621. {
  1622.   register struct command_line *cmds;
  1623.   register struct cmd_list_element *c, *newc;
  1624.   char *tem = comname;
  1625.  
  1626.   validate_comname (comname);
  1627.  
  1628.   /* Look it up, and verify that we got an exact match.  */
  1629.   c = lookup_cmd (&tem, cmdlist, "", -1, 1);
  1630.   if (c && 0 != strcmp (comname, c->name))
  1631.     c = 0;
  1632.     
  1633.   if (c)
  1634.     {
  1635.       if (c->class == class_user || c->class == class_alias)
  1636.     tem = "Redefine command \"%s\"? ";
  1637.       else
  1638.     tem = "Really redefine built-in command \"%s\"? ";
  1639.       if (!query (tem, c->name))
  1640.     error ("Command \"%s\" not redefined.", c->name);
  1641.     }
  1642.  
  1643.   comname = savestring (comname, strlen (comname));
  1644.  
  1645.   /* If the rest of the commands will be case insensetive, this one 
  1646.      should behave in the same manner. */
  1647.   for (tem = comname; *tem; tem++)
  1648.     if (isupper(*tem)) *tem = tolower(*tem);
  1649.  
  1650.   if (from_tty)
  1651.     {
  1652.       printf ("Type commands for definition of \"%s\".\n\
  1653. End with a line saying just \"end\".\n", comname);
  1654.       fflush (stdout);
  1655.     }
  1656.  
  1657.   cmds = read_command_lines ();
  1658.  
  1659.   if (c && c->class == class_user)
  1660.     free_command_lines (&c->user_commands);
  1661.  
  1662.   newc = add_cmd (comname, class_user, user_defined_command,
  1663.        (c && c->class == class_user)
  1664.        ? c->doc : savestring ("User-defined.", 13), &cmdlist);
  1665.   newc->user_commands = cmds;
  1666. }
  1667.  
  1668. static void
  1669. document_command (comname, from_tty)
  1670.      char *comname;
  1671.      int from_tty;
  1672. {
  1673.   struct command_line *doclines;
  1674.   register struct cmd_list_element *c;
  1675.   char *tem = comname;
  1676.  
  1677.   validate_comname (comname);
  1678.  
  1679.   c = lookup_cmd (&tem, cmdlist, "", 0, 1);
  1680.  
  1681.   if (c->class != class_user)
  1682.     error ("Command \"%s\" is built-in.", comname);
  1683.  
  1684.   if (from_tty)
  1685.     printf ("Type documentation for \"%s\".\n\
  1686. End with a line saying just \"end\".\n", comname);
  1687.  
  1688.   doclines = read_command_lines ();
  1689.  
  1690.   if (c->doc) free (c->doc);
  1691.  
  1692.   {
  1693.     register struct command_line *cl1;
  1694.     register int len = 0;
  1695.  
  1696.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1697.       len += strlen (cl1->line) + 1;
  1698.  
  1699.     c->doc = (char *) xmalloc (len + 1);
  1700.     *c->doc = 0;
  1701.  
  1702.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1703.       {
  1704.     strcat (c->doc, cl1->line);
  1705.     if (cl1->next)
  1706.       strcat (c->doc, "\n");
  1707.       }
  1708.   }
  1709.  
  1710.   free_command_lines (&doclines);
  1711. }
  1712.  
  1713. static void
  1714. print_gnu_advertisement()
  1715. {
  1716.     printf ("\
  1717. GDB is free software and you are welcome to distribute copies of it\n\
  1718.  under certain conditions; type \"show copying\" to see the conditions.\n\
  1719. There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
  1720. ");
  1721. }
  1722.  
  1723. static void
  1724. print_gdb_version ()
  1725. {
  1726.   printf_filtered ("\
  1727. GDB %s, Copyright 1992 Free Software Foundation, Inc.",
  1728.       version);
  1729. }
  1730.  
  1731. /* ARGSUSED */
  1732. static void
  1733. show_version (args, from_tty)
  1734.      char *args;
  1735.      int from_tty;
  1736. {
  1737.   immediate_quit++;
  1738.   print_gnu_advertisement ();
  1739.   print_gdb_version ();
  1740.   printf_filtered ("\n");
  1741.   immediate_quit--;
  1742. }
  1743.  
  1744. /* xgdb calls this to reprint the usual GDB prompt.  */
  1745.  
  1746. void
  1747. print_prompt ()
  1748. {
  1749.   printf ("%s", prompt);
  1750.   fflush (stdout);
  1751. }
  1752.  
  1753. static void
  1754. quit_command (args, from_tty)
  1755.      char *args;
  1756.      int from_tty;
  1757. {
  1758.   if (inferior_pid != 0 && target_has_execution)
  1759.     {
  1760.       if (query ("The program is running.  Quit anyway? "))
  1761.     {
  1762.       target_kill ();
  1763.     }
  1764.       else
  1765.     error ("Not confirmed.");
  1766.     }
  1767.   /* Save the history information if it is appropriate to do so.  */
  1768.   if (write_history_p && history_filename)
  1769.     write_history (history_filename);
  1770.   exit (0);
  1771. }
  1772.  
  1773. /* Returns whether GDB is running on a terminal and whether the user
  1774.    desires that questions be asked of them on that terminal.  */
  1775.  
  1776. int
  1777. input_from_terminal_p ()
  1778. {
  1779.   return gdb_has_a_terminal && (instream == stdin) & caution;
  1780. }
  1781.  
  1782. /* ARGSUSED */
  1783. static void
  1784. pwd_command (args, from_tty)
  1785.      char *args;
  1786.      int from_tty;
  1787. {
  1788.   if (args) error ("The \"pwd\" command does not take an argument: %s", args);
  1789.   getcwd (dirbuf, sizeof (dirbuf));
  1790.  
  1791.   if (strcmp (dirbuf, current_directory))
  1792.     printf ("Working directory %s\n (canonically %s).\n",
  1793.         current_directory, dirbuf);
  1794.   else
  1795.     printf ("Working directory %s.\n", current_directory);
  1796. }
  1797.  
  1798. static void
  1799. cd_command (dir, from_tty)
  1800.      char *dir;
  1801.      int from_tty;
  1802. {
  1803.   int len;
  1804.   int change;
  1805.  
  1806.   /* If the new directory is absolute, repeat is a no-op; if relative,
  1807.      repeat might be useful but is more likely to be a mistake.  */
  1808.   dont_repeat ();
  1809.  
  1810.   if (dir == 0)
  1811.     error_no_arg ("new working directory");
  1812.  
  1813.   dir = tilde_expand (dir);
  1814.   make_cleanup (free, dir);
  1815.  
  1816.   if (chdir (dir) < 0)
  1817.     perror_with_name (dir);
  1818.  
  1819.   len = strlen (dir);
  1820.   dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
  1821.   if (dir[0] == '/')
  1822.     current_directory = dir;
  1823.   else
  1824.     {
  1825.       current_directory = concat (current_directory, "/", dir, NULL);
  1826.       free (dir);
  1827.     }
  1828.  
  1829.   /* Now simplify any occurrences of `.' and `..' in the pathname.  */
  1830.  
  1831.   change = 1;
  1832.   while (change)
  1833.     {
  1834.       char *p;
  1835.       change = 0;
  1836.  
  1837.       for (p = current_directory; *p;)
  1838.     {
  1839.       if (!strncmp (p, "/./", 2)
  1840.           && (p[2] == 0 || p[2] == '/'))
  1841.         strcpy (p, p + 2);
  1842.       else if (!strncmp (p, "/..", 3)
  1843.            && (p[3] == 0 || p[3] == '/')
  1844.            && p != current_directory)
  1845.         {
  1846.           char *q = p;
  1847.           while (q != current_directory && q[-1] != '/') q--;
  1848.           if (q != current_directory)
  1849.         {
  1850.           strcpy (q-1, p+3);
  1851.           p = q-1;
  1852.         }
  1853.         }
  1854.       else p++;
  1855.     }
  1856.     }
  1857.  
  1858.   forget_cached_source_info ();
  1859.  
  1860.   if (from_tty)
  1861.     pwd_command ((char *) 0, 1);
  1862. }
  1863.  
  1864. /* ARGSUSED */
  1865. static void
  1866. source_command (args, from_tty)
  1867.      char *args;
  1868.      int from_tty;
  1869. {
  1870.   FILE *stream;
  1871.   struct cleanup *cleanups;
  1872.   char *file = args;
  1873.  
  1874.   if (file == 0)
  1875.     /* Let source without arguments read .gdbinit.  */
  1876.     file = gdbinit;
  1877.  
  1878.   file = tilde_expand (file);
  1879.   make_cleanup (free, file);
  1880.  
  1881.   stream = fopen (file, "r");
  1882.   if (stream == 0)
  1883.     perror_with_name (file);
  1884.  
  1885.   cleanups = make_cleanup (fclose, stream);
  1886.  
  1887.   read_command_file (stream);
  1888.  
  1889.   do_cleanups (cleanups);
  1890. }
  1891.  
  1892. /* ARGSUSED */
  1893. static void
  1894. echo_command (text, from_tty)
  1895.      char *text;
  1896.      int from_tty;
  1897. {
  1898.   char *p = text;
  1899.   register int c;
  1900.  
  1901.   if (text)
  1902.     while (c = *p++)
  1903.       {
  1904.     if (c == '\\')
  1905.       {
  1906.         /* \ at end of argument is used after spaces
  1907.            so they won't be lost.  */
  1908.         if (*p == 0)
  1909.           return;
  1910.  
  1911.         c = parse_escape (&p);
  1912.         if (c >= 0)
  1913.           printf_filtered ("%c", c);
  1914.       }
  1915.     else
  1916.       printf_filtered ("%c", c);
  1917.       }
  1918.  
  1919.   /* Force this output to appear now.  */
  1920.   wrap_here ("");
  1921.   fflush (stdout);
  1922. }
  1923.  
  1924. /* ARGSUSED */
  1925. static void
  1926. dump_me_command (args, from_tty)
  1927.      char *args;
  1928.      int from_tty;
  1929. {
  1930.   if (query ("Should GDB dump core? "))
  1931.     {
  1932.       signal (SIGQUIT, SIG_DFL);
  1933.       kill (getpid (), SIGQUIT);
  1934.     }
  1935. }
  1936.  
  1937. /* Functions to manipulate command line editing control variables.  */
  1938.  
  1939. /* Number of commands to print in each call to show_commands.  */
  1940. #define Hist_print 10
  1941. static void
  1942. show_commands (args, from_tty)
  1943.      char *args;
  1944.      int from_tty;
  1945. {
  1946.   /* Index for history commands.  Relative to history_base.  */
  1947.   int offset;
  1948.  
  1949.   /* Number of the history entry which we are planning to display next.
  1950.      Relative to history_base.  */
  1951.   static int num = 0;
  1952.  
  1953.   /* The first command in the history which doesn't exist (i.e. one more
  1954.      than the number of the last command).  Relative to history_base.  */
  1955.   int hist_len;
  1956.  
  1957.   extern struct _hist_entry *history_get PARAMS ((int));
  1958.   extern int history_base;
  1959.  
  1960.   /* Print out some of the commands from the command history.  */
  1961.   /* First determine the length of the history list.  */
  1962.   hist_len = history_size;
  1963.   for (offset = 0; offset < history_size; offset++)
  1964.     {
  1965.       if (!history_get (history_base + offset))
  1966.     {
  1967.       hist_len = offset;
  1968.       break;
  1969.     }
  1970.     }
  1971.  
  1972.   if (args)
  1973.     {
  1974.       if (args[0] == '+' && args[1] == '\0')
  1975.     /* "info editing +" should print from the stored position.  */
  1976.     ;
  1977.       else
  1978.     /* "info editing <exp>" should print around command number <exp>.  */
  1979.     num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
  1980.     }
  1981.   /* "show commands" means print the last Hist_print commands.  */
  1982.   else
  1983.     {
  1984.       num = hist_len - Hist_print;
  1985.     }
  1986.  
  1987.   if (num < 0)
  1988.     num = 0;
  1989.  
  1990.   /* If there are at least Hist_print commands, we want to display the last
  1991.      Hist_print rather than, say, the last 6.  */
  1992.   if (hist_len - num < Hist_print)
  1993.     {
  1994.       num = hist_len - Hist_print;
  1995.       if (num < 0)
  1996.     num = 0;
  1997.     }
  1998.  
  1999.   for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
  2000.     {
  2001.       printf_filtered ("%5d  %s\n", history_base + offset,
  2002.           (history_get (history_base + offset))->line);
  2003.     }
  2004.  
  2005.   /* The next command we want to display is the next one that we haven't
  2006.      displayed yet.  */
  2007.   num += Hist_print;
  2008.   
  2009.   /* If the user repeats this command with return, it should do what
  2010.      "show commands +" does.  This is unnecessary if arg is null,
  2011.      because "show commands +" is not useful after "show commands".  */
  2012.   if (from_tty && args)
  2013.     {
  2014.       args[0] = '+';
  2015.       args[1] = '\0';
  2016.     }
  2017. }
  2018.  
  2019. /* Called by do_setshow_command.  */
  2020. /* ARGSUSED */
  2021. static void
  2022. set_history_size_command (args, from_tty, c)
  2023.      char *args;
  2024.      int from_tty;
  2025.      struct cmd_list_element *c;
  2026. {
  2027.   if (history_size == UINT_MAX)
  2028.     unstifle_history ();
  2029.   else if (history_size >= 0)
  2030.     stifle_history (history_size);
  2031.   else
  2032.     {
  2033.       history_size = UINT_MAX;
  2034.       error ("History size must be non-negative");
  2035.     }
  2036. }
  2037.  
  2038. /* ARGSUSED */
  2039. static void
  2040. set_history (args, from_tty)
  2041.      char *args;
  2042.      int from_tty;
  2043. {
  2044.   printf ("\"set history\" must be followed by the name of a history subcommand.\n");
  2045.   help_list (sethistlist, "set history ", -1, stdout);
  2046. }
  2047.  
  2048. /* ARGSUSED */
  2049. static void
  2050. show_history (args, from_tty)
  2051.      char *args;
  2052.      int from_tty;
  2053. {
  2054.   cmd_show_list (showhistlist, from_tty, "");
  2055. }
  2056.  
  2057. int info_verbose = 0;        /* Default verbose msgs off */
  2058.  
  2059. /* Called by do_setshow_command.  An elaborate joke.  */
  2060. /* ARGSUSED */
  2061. static void 
  2062. set_verbose (args, from_tty, c)
  2063.      char *args;
  2064.      int from_tty;
  2065.      struct cmd_list_element *c;
  2066. {
  2067.   char *cmdname = "verbose";
  2068.   struct cmd_list_element *showcmd;
  2069.   
  2070.   showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
  2071.  
  2072.   if (info_verbose)
  2073.     {
  2074.       c->doc = "Set verbose printing of informational messages.";
  2075.       showcmd->doc = "Show verbose printing of informational messages.";
  2076.     }
  2077.   else
  2078.     {
  2079.       c->doc = "Set verbosity.";
  2080.       showcmd->doc = "Show verbosity.";
  2081.     }
  2082. }
  2083.  
  2084. static void
  2085. float_handler (signo)
  2086. int signo;
  2087. {
  2088.   /* This message is based on ANSI C, section 4.7.  Note that integer
  2089.      divide by zero causes this, so "float" is a misnomer.  */
  2090.   error ("Erroneous arithmetic operation.");
  2091. }
  2092.  
  2093. /* Return whether we are running a batch file or from terminal.  */
  2094. int
  2095. batch_mode ()
  2096. {
  2097.   return !(instream == stdin && ISATTY (stdin));
  2098. }
  2099.  
  2100.  
  2101. static void
  2102. initialize_cmd_lists ()
  2103. {
  2104.   cmdlist = (struct cmd_list_element *) 0;
  2105.   infolist = (struct cmd_list_element *) 0;
  2106.   enablelist = (struct cmd_list_element *) 0;
  2107.   disablelist = (struct cmd_list_element *) 0;
  2108.   deletelist = (struct cmd_list_element *) 0;
  2109.   enablebreaklist = (struct cmd_list_element *) 0;
  2110.   setlist = (struct cmd_list_element *) 0;
  2111.   showlist = NULL;
  2112.   sethistlist = (struct cmd_list_element *) 0;
  2113.   showhistlist = NULL;
  2114.   unsethistlist = (struct cmd_list_element *) 0;
  2115. }
  2116.  
  2117. /* Init the history buffer.  Note that we are called after the init file(s)
  2118.  * have been read so that the user can change the history file via his
  2119.  * .gdbinit file (for instance).  The GDBHISTFILE environment variable
  2120.  * overrides all of this.
  2121.  */
  2122.  
  2123. static void
  2124. initialize_history()
  2125. {
  2126.   char *tmpenv;
  2127.  
  2128.   tmpenv = getenv ("HISTSIZE");
  2129.   if (tmpenv)
  2130.     history_size = atoi (tmpenv);
  2131.   else if (!history_size)
  2132.     history_size = 256;
  2133.  
  2134.   stifle_history (history_size);
  2135.  
  2136.   tmpenv = getenv ("GDBHISTFILE");
  2137.   if (tmpenv)
  2138.     history_filename = savestring (tmpenv, strlen(tmpenv));
  2139.   else if (!history_filename) {
  2140.     /* We include the current directory so that if the user changes
  2141.        directories the file written will be the same as the one
  2142.        that was read.  */
  2143.     history_filename = concat (current_directory, "/.gdb_history", NULL);
  2144.   }
  2145.   read_history (history_filename);
  2146. }
  2147.  
  2148. static void
  2149. initialize_main ()
  2150. {
  2151.   struct cmd_list_element *c;
  2152.   
  2153. #ifdef DEFAULT_PROMPT
  2154.   prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT));
  2155. #else
  2156.   prompt = savestring ("(gdb) ", 6);
  2157. #endif
  2158.  
  2159.   /* Set the important stuff up for command editing.  */
  2160.   command_editing_p = 1;
  2161.   history_expansion_p = 0;
  2162.   write_history_p = 0;
  2163.   
  2164.   /* Setup important stuff for command line editing.  */
  2165.   rl_completion_entry_function = (int (*)()) symbol_completion_function;
  2166.   rl_completer_word_break_characters = gdb_completer_word_break_characters;
  2167.   rl_readline_name = "gdb";
  2168.  
  2169.   /* Define the classes of commands.
  2170.      They will appear in the help list in the reverse of this order.  */
  2171.  
  2172.   add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
  2173.   add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
  2174.   add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
  2175. The commands in this class are those defined by the user.\n\
  2176. Use the \"define\" command to define a command.", &cmdlist);
  2177.   add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
  2178.   add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
  2179.   add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
  2180.   add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
  2181.   add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
  2182.   add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
  2183. The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
  2184. counting from zero for the innermost (currently executing) frame.\n\n\
  2185. At any time gdb identifies one frame as the \"selected\" frame.\n\
  2186. Variable lookups are done with respect to the selected frame.\n\
  2187. When the program being debugged stops, gdb selects the innermost frame.\n\
  2188. The commands below can be used to select other frames by number or address.",
  2189.        &cmdlist);
  2190.   add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
  2191.  
  2192.   add_com ("pwd", class_files, pwd_command,
  2193.        "Print working directory.  This is used for your program as well.");
  2194.   add_com ("cd", class_files, cd_command,
  2195.        "Set working directory to DIR for debugger and program being debugged.\n\
  2196. The change does not take effect for the program being debugged\n\
  2197. until the next time it is started.");
  2198.  
  2199.   add_show_from_set
  2200.     (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt,
  2201.        "Set gdb's prompt",
  2202.        &setlist),
  2203.      &showlist);
  2204.   
  2205.   add_com ("echo", class_support, echo_command,
  2206.        "Print a constant string.  Give string as argument.\n\
  2207. C escape sequences may be used in the argument.\n\
  2208. No newline is added at the end of the argument;\n\
  2209. use \"\\n\" if you want a newline to be printed.\n\
  2210. Since leading and trailing whitespace are ignored in command arguments,\n\
  2211. if you want to print some you must use \"\\\" before leading whitespace\n\
  2212. to be printed or after trailing whitespace.");
  2213.   add_com ("document", class_support, document_command,
  2214.        "Document a user-defined command.\n\
  2215. Give command name as argument.  Give documentation on following lines.\n\
  2216. End with a line of just \"end\".");
  2217.   add_com ("define", class_support, define_command,
  2218.        "Define a new command name.  Command name is argument.\n\
  2219. Definition appears on following lines, one command per line.\n\
  2220. End with a line of just \"end\".\n\
  2221. Use the \"document\" command to give documentation for the new command.\n\
  2222. Commands defined in this way do not take arguments.");
  2223.  
  2224. #ifdef __STDC__
  2225.   add_com ("source", class_support, source_command,
  2226.        "Read commands from a file named FILE.\n\
  2227. Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
  2228. when gdb is started.");
  2229. #else
  2230.   /* Punt file name, we can't help it easily.  */
  2231.   add_com ("source", class_support, source_command,
  2232.        "Read commands from a file named FILE.\n\
  2233. Note that the file \".gdbinit\" is read automatically in this way\n\
  2234. when gdb is started.");
  2235. #endif
  2236.  
  2237.   add_com ("quit", class_support, quit_command, "Exit gdb.");
  2238.   add_com ("help", class_support, help_command, "Print list of commands.");
  2239.   add_com_alias ("q", "quit", class_support, 1);
  2240.   add_com_alias ("h", "help", class_support, 1);
  2241.  
  2242.  
  2243.   c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose,
  2244.            "Set ",
  2245.            &setlist),
  2246.   add_show_from_set (c, &showlist);
  2247.   c->function.sfunc = set_verbose;
  2248.   set_verbose (NULL, 0, c);
  2249.   
  2250.   add_com ("dump-me", class_obscure, dump_me_command,
  2251.        "Get fatal error; make debugger dump its core.");
  2252.  
  2253.   add_show_from_set
  2254.     (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p,
  2255.        "Set editing of command lines as they are typed.\n\
  2256. Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
  2257. Without an argument, command line editing is enabled.  To edit, use\n\
  2258. EMACS-like or VI-like commands like control-P or ESC.", &setlist),
  2259.      &showlist);
  2260.  
  2261.   add_prefix_cmd ("history", class_support, set_history,
  2262.           "Generic command for setting command history parameters.",
  2263.           &sethistlist, "set history ", 0, &setlist);
  2264.   add_prefix_cmd ("history", class_support, show_history,
  2265.           "Generic command for showing command history parameters.",
  2266.           &showhistlist, "show history ", 0, &showlist);
  2267.  
  2268.   add_show_from_set
  2269.     (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p,
  2270.        "Set history expansion on command input.\n\
  2271. Without an argument, history expansion is enabled.", &sethistlist),
  2272.      &showhistlist);
  2273.  
  2274.   add_show_from_set
  2275.     (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p,
  2276.        "Set saving of the history record on exit.\n\
  2277. Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
  2278. Without an argument, saving is enabled.", &sethistlist),
  2279.      &showhistlist);
  2280.  
  2281.   c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size,
  2282.            "Set the size of the command history, \n\
  2283. ie. the number of previous commands to keep a record of.", &sethistlist);
  2284.   add_show_from_set (c, &showhistlist);
  2285.   c->function.sfunc = set_history_size_command;
  2286.  
  2287.   add_show_from_set
  2288.     (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename,
  2289.        "Set the filename in which to record the command history\n\
  2290.  (the list of previous commands of which a record is kept).", &sethistlist),
  2291.      &showhistlist);
  2292.  
  2293.   add_show_from_set
  2294.     (add_set_cmd ("confirm", class_support, var_boolean,
  2295.           (char *)&caution,
  2296.           "Set whether to confirm potentially dangerous operations.",
  2297.           &setlist),
  2298.      &showlist);
  2299.  
  2300.     add_show_from_set
  2301.     (add_set_cmd ("icount", class_support, var_uinteger,
  2302.               (char *)&instCount,
  2303.               "Set instruction count.",
  2304.               &setlist),
  2305.      &showlist);
  2306.  
  2307.   add_prefix_cmd ("info", class_info, info_command,
  2308.         "Generic command for showing things about the program being debugged.",
  2309.           &infolist, "info ", 0, &cmdlist);
  2310.   add_com_alias ("i", "info", class_info, 1);
  2311.  
  2312.   add_prefix_cmd ("show", class_info, show_command,
  2313.           "Generic command for showing things about the debugger.",
  2314.           &showlist, "show ", 0, &cmdlist);
  2315.   /* Another way to get at the same thing.  */
  2316.   add_info ("set", show_command, "Show all GDB settings.");
  2317.  
  2318.   add_cmd ("commands", no_class, show_commands,
  2319.        "Show the the history of commands you typed.\n\
  2320. You can supply a command number to start with, or a `+' to start after\n\
  2321. the previous command number shown.",
  2322.        &showlist);
  2323.  
  2324.   add_cmd ("version", no_class, show_version,
  2325.        "Show what version of GDB this is.", &showlist);
  2326. }
  2327.